﻿using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using Accona.Monitoring.AppStatistics;
using Accona.Monitoring.Publishers;
using Newtonsoft.Json;

namespace Accona.Monitoring.Exposure.REST
{
    public class RESTServer
    {
        private WebServiceHost _serviceHost;
        private readonly int _port;
        private string _uri;

        public RESTServer(int port)
        {
            this._port = port;
        }

        public RESTServer(string uri)
        {
            this._uri = uri;
        }

        public void Start()
        {
            if (_serviceHost!=null)
                throw new ArgumentException("Service allready started");
            var service = new MonitoringRESTService();
            if (_uri == null)
                _uri = string.Format("http://127.0.0.1:{0}/Monitoring/", _port);
            _serviceHost = new WebServiceHost(service, new Uri(_uri));
            _serviceHost.Open();
        }

        public void Shutdown()
        {
            _serviceHost.Close();
        }
    }

    [ServiceContract(Name = "MonitoringRESTService")]
    public interface IMonitoringRESTInterface
    {
        [OperationContract]
        [WebGet(UriTemplate = Routing.Route , ResponseFormat = WebMessageFormat.Json)]
        string GetStatisticsByAppName(string name);

        [OperationContract]
        [WebGet(UriTemplate = Routing.FullDefRoute, ResponseFormat = WebMessageFormat.Json)]
        string GetStatisticsByFullQualification(string appName, string statNamespace, string tag);
    }

    
    public static class Routing
    {
        public const string Route  = "ByName/{name}";
        public const string FullDefRoute = "ByFullDef/{appName}/{statNamespace}/{tag}";
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single,
                 ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MonitoringRESTService : IMonitoringRESTInterface
    {
        public string GetStatisticsByAppName(string name)
        {
            var result = JsonConvert.SerializeObject(new DefaultAppStatistics()
                                                         {
                                                             AppName = name,
                                                             MaxValue = 100.34
                                                         });
            return result;           
        }

        public string GetStatisticsByFullQualification(string appName, string statNamespace, string tag)
        {
            var memoryPublisher = Monitor.GetPublisher<MemoryPublisher>();
            var stat = memoryPublisher.GetStatistics(appName, statNamespace, tag);
            var result = JsonConvert.SerializeObject(stat);
            return result;
        }
    }

}
